package com.wokeMy.view.channe.changjie;

import android.app.Dialog;
import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.html.RhkjzfHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class CjieKjzfActivity extends BaseActivity implements View.OnClickListener{
    ImageView back_cjiekjzf_iv;
    RelativeLayout zfcard_cjiekjzf_rl;
    Button zhifu_cjiekjzf_bt;
    TextView khk_cjiekjzf_tv,hqyzm_cjiekjzf_tv;
    EditText yzm_cjiekjzf_et;

    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,bankCode;
    String user_id;
    int way_id;
    int payWay;
    String orderId,payNo,orderId_bind;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone;
    float amount;
    String orderNum;
    String msgCode,smsCode_bind;

    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_cjie_kjzf);
    }

    @Override
    public void initActivity() {
        back_cjiekjzf_iv.setOnClickListener(this);
        zfcard_cjiekjzf_rl.setOnClickListener(this);
        zhifu_cjiekjzf_bt.setOnClickListener(this);
        hqyzm_cjiekjzf_tv.setOnClickListener(this);
    }

    @Override
    public void initView() {

        back_cjiekjzf_iv = myFindViewById(R.id.back_cjiekjzf_iv);
        zfcard_cjiekjzf_rl = myFindViewById(R.id.zfcard_cjiekjzf_rl);
        khk_cjiekjzf_tv = myFindViewById(R.id.khk_cjiekjzf_tv);
        zhifu_cjiekjzf_bt = myFindViewById(R.id.zhifu_cjiekjzf_bt);
        yzm_cjiekjzf_et = myFindViewById(R.id.yzm_cjiekjzf_et);
        hqyzm_cjiekjzf_tv = myFindViewById(R.id.hqyzm_cjiekjzf_tv);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        user_id=datas_load.getId();

        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        amount=intent.getFloatExtra ("money", 0.01f);
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        payWay=unionPayChannel.getKind();
        String name="名字为空";
        if (!datas_load.getCom_name().equals("")){
            name=datas_load.getCom_name();
        }else {
            name=datas_load.getUser_nicename();
        }
        useName=name;
        merchantName=getResources().getString(R.string.oem_app_name)+"的产品";
        merchantBillName="购买"+getResources().getString(R.string.oem_app_name)+"的产品";
        legalPersonID=datas_load.getUser_no();
        merchantPersonPhone=datas_load.getUser_login();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_cjiekjzf_iv:
                finish();
                break;
            case R.id.hqyzm_cjiekjzf_tv:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(CjieKjzfActivity.this,"请选择银行卡",Toast.LENGTH_SHORT).show();
                }else {
                    if (!yzmfastClick()){
                        hqYzm();
                        /** 倒计时60秒，一次1秒 */
                        CountDownTimer timer = new CountDownTimer(60*1000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                // TODO Auto-generated method stub
                                hqyzm_cjiekjzf_tv.setText(" "+millisUntilFinished/1000+"秒"+" ");
                            }

                            @Override
                            public void onFinish() {
                                hqyzm_cjiekjzf_tv.setText("获取验证码");
                            }
                        }.start();
                    }
                }

                break;
            case R.id.zfcard_cjiekjzf_rl:
                Intent intent = new Intent (CjieKjzfActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "rhkjzf");
                startActivityForResult (intent, Constant.REQUEST_RHKJZF);
                break;
            case R.id.zhifu_cjiekjzf_bt:
                msgCode=yzm_cjiekjzf_et.getText().toString().trim();
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(this,"请选择支付卡",Toast.LENGTH_SHORT).show();
                }else if(msgCode==null||msgCode.equals("")) {
                    Toast.makeText(this,"请填写验证码",Toast.LENGTH_SHORT).show();
                }else{
                    if (!fastClick()) {
                        dialog = Util.createLoadingDialog(CjieKjzfActivity.this);
                        cjiZhifu();
                    }
                }

                break;
            default:
                break;
        }
    }
    //Todo
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constant.RESPONSE_RHKJZF){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            khk_cjiekjzf_tv.setText(pay_card_no);
            if(way_id==77){
                dialog=Util.createLoadingDialog(CjieKjzfActivity.this);
                Opencjiekjzf();
            }else if(way_id==78){
//                OpencjiekjzfSlv();
                hqBindCardYzm();
            }
        }else if (resultCode==Constant.RESPONSE_CJKJZFBIND){
            dialog=Util.createLoadingDialog(CjieKjzfActivity.this);
            smsCode_bind=data.getStringExtra("smsCode_bind");
            OpencjiekjzfSlv();
        }
    }

    //开通畅捷快捷支付(山高)
    public void Opencjiekjzf(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("bankAccountNo",pay_card_no);
        Log.e("//开通畅捷快捷支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.OPENCJKJZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("开通畅捷快捷支付", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        if (way_id==77){
                            if (jsonObject.getString("flag").equals("success")) {
                                String rhkjzfhtml = jsonObject.getJSONObject("data").getJSONObject("data").getJSONObject("data").getString("html");
                                    Intent intent =new Intent(CjieKjzfActivity.this, RhkjzfHtmlActivity.class);
                                    intent.putExtra("rhkjzfhtml",rhkjzfhtml);
                                    startActivity(intent);
                            }else{
                                tishi(jsonObject.getString("info"));
                            }
                        }else if(way_id==77){

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });

    }
    //开通畅捷快捷支付(商旅)
    public void OpencjiekjzfSlv(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("bankAccountNo",pay_card_no);
        params.put("smsCode",smsCode_bind);
        params.put("orderId",orderId_bind);
        Log.e("//开通畅捷快捷支付(商旅)params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.OPENCJKJZFSLV_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("开通畅捷快捷支付(商旅)", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data").getJSONObject("data");
                            if (jsonObject1.getString("respcode").equals("000000")) {
                                //签约状态 0 待开通 1 等待签约中 2 开通成功 3 开通失败 4 绑卡状态失效
                                String state;
                                switch (jsonObject1.getInt("activateStatus")) {
                                    case 0:
                                        state="待开通";
                                        break;
                                    case 1:
                                        state="等待签约中";
                                        break;
                                    case 2:
                                        state="开通成功";
                                        break;
                                    case 3:
                                        state="开通失败";
                                        break;
                                    case 4:
                                        state="绑卡状态失效";
                                        break;
                                    default:
                                        state="default";
                                        break;
                                }
                                Toast.makeText(CjieKjzfActivity.this,state,Toast.LENGTH_SHORT).show();

                            }else{
                                Toast.makeText(CjieKjzfActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                            }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });

    }
    // 获取畅捷验证码
    public void  hqYzm(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",amount);
        params.put("bankAccountNo",pay_card_no);
        //        params.put("creditCardNo","6225757545273378");

        Log.e("获取畅捷验证码",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HQYZMCJIE_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("获取畅捷验证码///", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        if (jsonObject.getJSONObject("data").getJSONObject("data").getString("code").equals("000000")){
                            orderId=jsonObject1.getJSONObject("data").getString("orderid");
                            payNo=jsonObject1.getJSONObject("data").getString("payno");
                            Log.e("获取畅捷验证码",orderId+"//"+payNo);
                            Toast.makeText(CjieKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(CjieKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }

    ///畅捷确认支付
    public void cjiZhifu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("amount",amount);
        params.put("bankAccountNo",pay_card_no);
        params.put("orderId",orderId);//发送短信后返回
        params.put("payNo",payNo);//发送短信后返回
        params.put("smsCode",msgCode);
        params.put("productName",merchantName);
        params.put("productDesc",merchantBillName);
        //北京融汇快捷支付填写完验证码后确定支付
        //        params.put("creditCardNo","6225757545273378");
        Log.e("/畅捷确认支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CJIEZHIFU_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("/畅捷确填写完验证码后确定支付", result);
                    JSONObject  jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data").getJSONObject("data");
                            String respcode=jsonObject1.getString("respcode");
                            if (respcode.equals("000000")){
                                Toast.makeText(CjieKjzfActivity.this,"支付成功",Toast.LENGTH_LONG).show();
                                finish();
                            }else {
                                tishi(jsonObject1.getString("respmsg"));
                                //                            Toast.makeText(CjieKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            tishi(jsonObject.getString("info"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }




                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });

    }
    // 获取畅捷侧开通短信验证码
    public void  hqBindCardYzm(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",78); //商旅
        params.put("bankAccountNo",pay_card_no);
        //        params.put("creditCardNo","6225757545273378");

        Log.e("获取畅捷获取畅捷侧开通短信验证码",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HQCBKYZM_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    Util.closeDialog(dialog);
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("获取获取畅捷侧开通短信验证码///", result);
                    try {

                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("unknown")){
                                tishi(jsonObject.getString("info"));
                        }else if(jsonObject.getString("info").equals("银行卡已绑定")){

                        }else {
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                            if (jsonObject.getJSONObject("data").getJSONObject("data").getString("code").equals("000000")){
                                orderId_bind=jsonObject1.getJSONObject("data").getString("orderid");
                                Log.e("获取畅捷验证码",orderId_bind+"//");
                                Intent intent =new Intent(CjieKjzfActivity.this, CjSlyzmActivity.class);
                                intent.putExtra("jiemian","cjieyzm");
                                startActivityForResult (intent, Constant.REQUEST_CJKJZFBIND);
                                //                            Toast.makeText(CjSlyzmActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(CjieKjzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
}
